//
//  ViewController.swift
//  Attitude3D
//
//  Created by CmST0us on 16/7/11.
//  Copyright © 2016年 广州睿航电子科技有限公司. All rights reserved.
//

import UIKit
import QuartzCore
import SceneKit
import CoreMotion

class ViewController: UIViewController {
    
    @IBOutlet var sceneView: SCNView!
    var motionManager: CMMotionManager?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // create a new scene
        let scene = SCNScene(named: "art.scnassets/ROV.scn")!
        
        // create and add a camera to the scene
//        let cameraNode = SCNNode()
//        cameraNode.camera = SCNCamera()
//        scene.roo
////        scene.rootNode.addChildNode(cameraNode)
//        
//        // place the camera
//        cameraNode.eulerAngles = SCNVector3(x: 0, y: Float(M_PI), z: 0)
//        cameraNode.position = SCNVector3(x: 0, y: 15, z: -15)
//        
        
        // create and add a light to the scene
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light!.type = SCNLightTypeOmni
        lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
        scene.rootNode.addChildNode(lightNode)
        
        // create and add an ambient light to the scene
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light!.type = SCNLightTypeAmbient
        ambientLightNode.light!.color = UIColor.darkGrayColor()
        scene.rootNode.addChildNode(ambientLightNode)
        
        // retrieve the ship node
        let ship = scene.rootNode.childNodeWithName("ROV", recursively: true)!
//        let ball = scene.rootNode.childNodeWithName("ball", recursively: true)!
        // animate the 3d object
        //        ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))
        
        // retrieve the SCNView
        self.sceneView.scene = scene
        self.sceneView.allowsCameraControl = false
        self.sceneView.showsStatistics = false
        self.sceneView.backgroundColor = UIColor.clearColor()
        
        let yawLabelFrame = CGRectMake(0, 0, 50, 20)
        let yawLabel  = UILabel.init(frame: yawLabelFrame)
        yawLabel.font = UIFont(name: "He", size: 12)
        sceneView.addSubview(yawLabel)
        
        motionManager = CMMotionManager()
        motionManager?.deviceMotionUpdateInterval = 0.1
        motionManager?.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (deviceMotion, error) in
            //            let yaw = self.rToD((deviceMotion?.attitude.yaw)!)
            //            let pitch = self.rToD((deviceMotion?.attitude.pitch)!)
            //            let roll = self.rToD((deviceMotion?.attitude.roll)!)
            let yaw = deviceMotion?.attitude.yaw
            let pitch = deviceMotion?.attitude.pitch
            let roll = deviceMotion?.attitude.roll
            print("yaw: \(yaw)\troll: \(roll)\tpitch: \(pitch)")
            yawLabel.text = "\(String.init(format: "%.2f", yaw!))"
            //            ship.runAction(SCNAction.rotateByX(CGFloat(pitch), y: CGFloat(roll), z: CGFloat(yaw), duration: 0.5))
            SCNTransaction.begin()
            SCNTransaction.setAnimationDuration(0.4)
            ship.eulerAngles = SCNVector3(x: Float(-roll!), y: Float(pitch!), z: 0)
//            ball.eulerAngles = SCNVector3(x: 0, y: Float(-yaw!), z: 0)
            SCNTransaction.commit()
        })
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
